package gemini.castor.ui.client.page.widget.productpreview;

import gemini.basic.model.Product;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.HasHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Panel;

public class ProductPreviewWidget extends Composite implements HasHandlers{

	@UiTemplate("ProductPreviewWidget.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, ProductPreviewWidget> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

	public ProductPreviewWidget() {
		initWidget(binder.createAndBindUi(this));
	}

	@UiField
	Image imgProduct;
	
	@UiField
	HTML htmName;
	
	@UiField
	HTML htmPrice;
	
	@UiHandler("imgProduct")
	void onClickImage(ClickEvent e){
		fireEvent(new ProductPreviewEvent(ProductPreviewEvent.ForwardType.SELECT));
	}
	
	public void loadData(Product product){
		imgProduct.setUrl(product.getImageUrl());
		htmName.setHTML(product.getName());
		htmPrice.setHTML(String.valueOf(product.getVolumePoint()));
	}
	
	public void addProductPreviewHandler(ProductPreviewHandler handler){
		this.addHandler(handler, ProductPreviewEvent.getType());
	}
}
